{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1030c7c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[210100/12907]\n",
      " [188400/12907]\n",
      " [183300/12907]\n",
      " [223400/12907]]\n"
     ]
    }
   ],
   "source": [
    "#例3-3\n",
    "import numpy as np\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "A=np.array([[9,-2,0,-1],\n",
    "            [-2,12,-3,0],\n",
    "            [0,-3,15,-4],\n",
    "            [-1,0,-4,10]])\n",
    "b=np.array([100,100,100,100])\n",
    "X=np.linalg.solve(A, b)\n",
    "print(X.reshape(4,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bda4b172",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [0]]\n"
     ]
    }
   ],
   "source": [
    "#练习3-2\n",
    "import numpy as np\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "A=np.array([[1,2],\n",
    "            [2,5]])\n",
    "b=np.array([1,2])\n",
    "X=np.linalg.solve(A, b)\n",
    "print(X.reshape(2,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4f8fd56a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 5]\n",
      " [0 1 0 0]\n",
      " [0 0 1 3]]\n",
      "3\n",
      "[0 1 2]\n"
     ]
    }
   ],
   "source": [
    "#例3-7、例3-8\n",
    "import numpy as np\n",
    "from utility import rowLadder,simplestLadder\n",
    "A=np.array([[1,-1,-1],\n",
    "            [2,-1,-3],\n",
    "            [3,2,-5]],dtype='float')\n",
    "b=np.array([2,1,0])\n",
    "B=np.hstack((A,b.reshape(3,1)))\n",
    "rank, order=rowLadder(B,3,3)\n",
    "simplestLadder(B,rank)\n",
    "print(B)\n",
    "print(rank)\n",
    "print(order)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "95fe7f86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 -1 4]\n",
      " [0 1 0 -1 3]\n",
      " [0 0 1 0 -3]\n",
      " [0 0 0 0 0]]\n",
      "3\n",
      "[0 1 3 2]\n"
     ]
    }
   ],
   "source": [
    "#练习3-6、练习3-7\n",
    "import numpy as np\n",
    "from utility import rowLadder,simplestLadder\n",
    "from fractions import Fraction as F\n",
    "A=np.array([[2,-1,-1,1],\n",
    "            [1,1,-2,1],\n",
    "            [4,-6,2,-2],\n",
    "            [3,6,-9,7]],dtype='float')\n",
    "b=np.array([2,4,4,9])\n",
    "B=np.hstack((A,b.reshape(4,1)))\n",
    "rank, order=rowLadder(B,4,4)\n",
    "simplestLadder(B,rank)\n",
    "print(B)\n",
    "print(rank)\n",
    "print(order)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "94bd3eb3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 3/17 -13/17]\n",
      " [0 19/17 -20/17]\n",
      " [0 1 0]\n",
      " [0 0 1]]\n",
      "[[0 2 5/3]\n",
      " [0 -2 -4/3]\n",
      " [0 1 0]\n",
      " [0 0 1]]\n"
     ]
    }
   ],
   "source": [
    "#例3-13、练习3-12\n",
    "import numpy as np\n",
    "from utility import mySolve\n",
    "from fractions import Fraction as Q\n",
    "np.set_printoptions(formatter=\n",
    "                    {'all':lambda x:str(Q(x).limit_denominator())})\n",
    "A=np.array([[3,4,-5,7],\n",
    "           [2,-3,3,-2],\n",
    "           [4,11,-13,16],\n",
    "           [7,-2,1,3]],dtype='float')\n",
    "b=np.array([0,0,0,0])\n",
    "s=mySolve(A,b)\n",
    "print(s)\n",
    "A=np.array([[1,2,2,1],\n",
    "           [2,1,-2,-2],\n",
    "           [1,-1,-4,-3]],dtype='float')\n",
    "b=np.array([0,0,0])\n",
    "s=mySolve(A,b)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0cb4577c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5]\n",
      " [0]\n",
      " [3]]\n",
      "[[1]\n",
      " [0]\n",
      " [0]]\n"
     ]
    }
   ],
   "source": [
    "#例3-14、练习3-13\n",
    "import numpy as np\n",
    "from utility import mySolve,Q\n",
    "np.set_printoptions(formatter=\n",
    "                    {'all':lambda x: str(Q(x).limit_denominator())})\n",
    "A=np.array([[1,-1,-1],              #系数矩阵\n",
    "            [2,-1,-3],\n",
    "            [3,2,-5]],dtype='float')\n",
    "b=np.array([2,1,0])                 #常数矩阵\n",
    "s=mySolve(A,b)\n",
    "print(s)\n",
    "A=np.array([[1,2,3],              #系数矩阵\n",
    "            [2,1,5],\n",
    "            [3,5,1]],dtype='float')\n",
    "b=np.array([1,2,3])                 #常数矩阵\n",
    "s=mySolve(A,b)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "da97b208",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "#例3-15、练习3-14\n",
    "import numpy as np\n",
    "from utility import Q\n",
    "from utility import mySolve\n",
    "np.set_printoptions(formatter={'all':lambda x: str(Q(x).limit_denominator())})\n",
    "A=np.array([[4,2,-1],              #系数矩阵\n",
    "            [3,-1,2],\n",
    "            [11,3,0]],dtype='float')\n",
    "b=np.array([2,10,8])               #常数矩阵\n",
    "s=mySolve(A,b)\n",
    "print(s)\n",
    "A=np.array([[1,-2,3,-1],              #系数矩阵\n",
    "            [3,-1,5,-3],\n",
    "            [2,1,2,-2]],dtype='float')\n",
    "b=np.array([1,2,3])               #常数矩阵\n",
    "s=mySolve(A,b)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c2245d03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1/2 1/2 -1/2]\n",
      " [0 0 1]\n",
      " [0 1 0]\n",
      " [0 0 0]]\n",
      "[[6/7 1/7 1/7]\n",
      " [-5/7 5/7 -9/7]\n",
      " [0 1 0]\n",
      " [0 0 1]]\n"
     ]
    }
   ],
   "source": [
    "#例3-16、练习3-15\n",
    "import numpy as np\n",
    "from utility import Q\n",
    "from utility import mySolve\n",
    "np.set_printoptions(formatter={'all':lambda x: str(Q(x).limit_denominator())})\n",
    "A=np.array([[2,1,-1,1],\n",
    "            [4,2,-2,1],\n",
    "            [2,1,-1,-1]],dtype='float')\n",
    "b=np.array([1,2,1])\n",
    "s=mySolve(A,b)\n",
    "print(s)\n",
    "A=np.array([[2,1,-1,1],\n",
    "            [3,-2,1,-3],\n",
    "            [1,4,-3,5]],dtype='float')\n",
    "b=np.array([1,4,-2])\n",
    "s=mySolve(A,b)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb11d5b1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
